package com.acme.bankapp.domain.bank;

import java.io.Serializable;

import com.acme.bankapp.domain.bank.exceptions.NotEnoughFundsException;

public abstract class AbstractAccount implements Account, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -95733712442601271L;
	static int currentID=0;
	
	protected double balance;
	private final int id;
	
	
	public AbstractAccount(double balance) {
		if (balance < 0) {
			throw new IllegalArgumentException(
					"Initial balance should be non-negative");
		}
		this.balance = balance;
		this.id = currentID++;
	}

	public AbstractAccount() {
		this(0);
	}

	@Override
	public double getBalance() {
		return balance;
	}

	@Override
	public double deposit(double sum) throws IllegalArgumentException {
		if (sum <= 0) {
			throw new IllegalArgumentException(
					"Deposit amount should be positive");
		}
			
		balance += sum;

		return balance;
	}

	@Override
	public long decimalValue(){
		return Math.round(this.balance);
	}
	
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (id != other.id)
			return false;
		return true;
	}

	abstract public void withdraw(double sum) throws NotEnoughFundsException;

	abstract public double maximumAmountToWithdraw();

	public int getId() {
		return id;
	}
}
